/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lfk.justwetools.Utils;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.aafwk.ability.delegation.IAbilityDelegator;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

public class SpUtilsTest {

    private static IAbilityDelegator sAbilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();

    @Test
    public void put() {
        String testValue = "testvalue";
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey", testValue);
        String value = (String) SpUtils.get(sAbilityDelegator.getAppContext(), "testkey", "");
        assertEquals(testValue, value);
    }

    @Test
    public void get() {
        String testValue = "testvalue";
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey", testValue);
        String value = (String) SpUtils.get(sAbilityDelegator.getAppContext(), "testkey", "");
        assertEquals(testValue, value);
    }

    @Test
    public void putList() {
        List<String> testValue = new ArrayList<>();
        testValue.add("fist");
        testValue.add("second");
        SpUtils.putList(sAbilityDelegator.getAppContext(), testValue, "testkey");
        List<?> value = SpUtils.getList(sAbilityDelegator.getAppContext(), "testkey");
        assertEquals(testValue.size(), value.size());
        assertEquals(testValue.get(0), value.get(0));
        assertEquals(testValue.get(1), value.get(1));
    }

    @Test
    public void getList() {
        List<String> testValue = new ArrayList<>();
        testValue.add("fist");
        testValue.add("second");
        SpUtils.putList(sAbilityDelegator.getAppContext(), testValue, "testkey");
        List<?> value = SpUtils.getList(sAbilityDelegator.getAppContext(), "testkey");
        assertEquals(testValue.size(), value.size());
        assertEquals(testValue.get(0), value.get(0));
        assertEquals(testValue.get(1), value.get(1));
    }

    @Test
    public void putMap() {
        Map<String, String> testValue = new HashMap<>();
        testValue.put("fist", "firstValue");
        testValue.put("second", "SecondValue");
        SpUtils.putMap(sAbilityDelegator.getAppContext(), testValue, "testkey");
        Map<?, ?> value = SpUtils.getMap(sAbilityDelegator.getAppContext(), "testkey");
        assertEquals(testValue.size(), value.size());
        assertEquals(testValue.get("first"), value.get("first"));
        assertEquals(testValue.get("second"), value.get("second"));
    }

    @Test
    public void getMap() {
        Map<String, String> testValue = new HashMap<>();
        testValue.put("fist", "firstValue");
        testValue.put("second", "SecondValue");
        SpUtils.putMap(sAbilityDelegator.getAppContext(), testValue, "testkey");
        Map<?, ?> value = SpUtils.getMap(sAbilityDelegator.getAppContext(), "testkey");
        assertEquals(testValue.size(), value.size());
        assertEquals(testValue.get("first"), value.get("first"));
        assertEquals(testValue.get("second"), value.get("second"));
    }

    @Test
    public void remove() {
        String testValue = "testvalue";
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey", testValue);
        SpUtils.remove(sAbilityDelegator.getAppContext(), "testkey");
        String value = (String) SpUtils.get(sAbilityDelegator.getAppContext(), "testkey", "");
        assertEquals("", value);
    }

    @Test
    public void clear() {
        String testValue = "testvalue";
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey1", testValue);
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey2", testValue);
        SpUtils.clear(sAbilityDelegator.getAppContext());
        String value1 = (String) SpUtils.get(sAbilityDelegator.getAppContext(), "testkey1", "");
        assertEquals("", value1);
        String value2 = (String) SpUtils.get(sAbilityDelegator.getAppContext(), "testkey2", "");
        assertEquals("", value2);
    }

    @Test
    public void contains() {
        String testValue = "testvalue";
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey1", testValue);
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey2", testValue);
        boolean has1 = SpUtils.contains(sAbilityDelegator.getAppContext(), "testkey1");
        assertTrue(has1);
        boolean has2 = SpUtils.contains(sAbilityDelegator.getAppContext(), "testkey2");
        assertTrue(has2);
    }

    @Test
    public void getAll() {
        String testValue1 = "testvalue1";
        String testValue2 = "testvalue2";
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey1", testValue1);
        SpUtils.put(sAbilityDelegator.getAppContext(), "testkey2", testValue2);
        SpUtils.clear(sAbilityDelegator.getAppContext());
        Map<String, ?> allValues = SpUtils.getAll(sAbilityDelegator.getAppContext());
        assertNull(allValues.get("testkey1"));
        assertNull(allValues.get("testkey2"));
    }
}